package vcp.client.GUI.mvc.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.regex.Pattern;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.text.JTextComponent;

import vcp.client.GUI.events.ValidationDescriptionEvent;
import vcp.client.GUI.mvc.controller.UIRootController;
import vcp.client.GUI.mvc.view.custom.CustomInputVerifier;
import vcp.client.GUI.mvc.view.effects.ValidationLayerUI;
import vcp.client.interfaces.listeners.IValidationListener;
import vcp.client.utility.Common;
import vcp.client.utility.Common.RegexStrings;
import vcp.client.utility.Common.UserNotification;
import vcp.client.utility.UtilityHelper;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;
/**
 * <p> user interface check in screen class </p>
 * @author VCP team
 *
 */
public class UICheckInView extends UIAbstractView
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JFormattedTextField ftfCarId;
	private JFormattedTextField ftfId;
	private JButton btnCancel;
	private JButton btnSubmit;

	private JLabel siteName;
	/**
	 * getter
	 * @return site name
	 */
	private JLabel getSiteName ()
	{
		if ( this.siteName == null )
		{
			this.siteName = new JLabel();
		}
		return this.siteName;
	}
	/**
	 * setter
	 * @param siteName
	 */
	public void setSiteName ( String siteName )
	{
		siteName = String.format("Site Name:%s", siteName);
		this.siteName = new JLabel(siteName);
		this.siteName.setFont(new Font("Lucida Grande", Font.BOLD, 12));
	}

	private IValidationListener validationListener;
	/**
	 * setter
	 * @param validationListener
	 */
	public void setValidationListener ( IValidationListener validationListener )
	{
		this.validationListener = validationListener;
	}

	private ValidationLayerUI carIDValidationLayerUI;
	private ValidationLayerUI idValidationLayerUI;
	/**
	 * contructor
	 */
	public UICheckInView ()
	{
		super();

		this.resetView();
	}

	/****************************************************************************************************************************
	 *** UIAbstractView methods ***
	 ****************************************************************************************************************************/
	@Override
	public ArrayList <JComponent> getAdditionalToolbarComponents ()
	{
		this.getAdditionalComponents().clear();
		this.getAdditionalComponents().add(this.getSiteName());

		return this.getAdditionalComponents();
	}

	@Override
	protected void setupLayout ()
	{
		this.setLayout(new BorderLayout(0, 0));

		JPanel panel_2 = new JPanel();
		this.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new GridLayout(2, 0, 0, 0));

		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3);
		panel_3.setLayout(new GridLayout(3, 1, 0, 0));

		JPanel panel_4 = new JPanel();
		panel_3.add(panel_4);

		JPanel panel_6 = new JPanel();
		panel_3.add(panel_6);
		panel_6.setLayout(new GridLayout(2, 1, 0, 0));

		JPanel panel_11 = new JPanel();
		panel_6.add(panel_11);
		panel_11.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblId = new JLabel("ID:");
		panel_11.add(lblId);
		lblId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblId.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		this.ftfId = new JFormattedTextField();
		this.ftfId.setToolTipText("Format:##...##");
		JLayer <JTextComponent> idLayer = new JLayer <JTextComponent>(this.ftfId);
		this.idValidationLayerUI = new ValidationLayerUI();
		idLayer.setUI(this.idValidationLayerUI);
		panel_11.add(idLayer);
		this.ftfId.setPreferredSize(new Dimension(200, 55));
		this.ftfId.setFont(new Font("Lucida Grande", Font.PLAIN, 32));

		JPanel panel_12 = new JPanel();
		panel_6.add(panel_12);
		panel_12.setLayout(new GridLayout(0, 3, 0, 0));

		JLabel lblCarId = new JLabel("Car ID:");
		panel_12.add(lblCarId);
		lblCarId.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCarId.setFont(new Font("Lucida Grande", Font.BOLD, 32));

		this.ftfCarId = new JFormattedTextField();
		JLayer <JTextComponent> carIDLayer = new JLayer <JTextComponent>(this.ftfCarId);
		this.carIDValidationLayerUI = new ValidationLayerUI();
		carIDLayer.setUI(this.carIDValidationLayerUI);
		this.ftfCarId.setToolTipText("Format:#######");
		panel_12.add(carIDLayer);
		this.ftfCarId.setPreferredSize(new Dimension(200, 55));
		this.ftfCarId.setFont(new Font("Lucida Grande", Font.PLAIN, 32));

		JPanel panel_10 = new JPanel();
		panel_2.add(panel_10);
		panel_10.setLayout(new GridLayout(4, 0, 0, 0));

		JPanel panel_5 = new JPanel();
		panel_10.add(panel_5);
		panel_5.setLayout(new GridLayout(0, 3, 0, 0));

		JPanel panel_7 = new JPanel();
		FlowLayout fl_panel_7 = (FlowLayout) panel_7.getLayout();
		fl_panel_7.setAlignment(FlowLayout.RIGHT);
		panel_5.add(panel_7);

		this.btnCancel = new JButton(Common.BTN_CANCEL);
		panel_7.add(this.btnCancel);
		this.btnCancel.setFocusable(false);
		this.btnCancel.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnCancel.setName(Common.BTN_CANCEL);
		this.btnCancel.setPreferredSize(new Dimension(175, 75));

		JPanel panel_8 = new JPanel();
		panel_5.add(panel_8);

		JPanel panel_9 = new JPanel();
		FlowLayout fl_panel_9 = (FlowLayout) panel_9.getLayout();
		fl_panel_9.setAlignment(FlowLayout.LEFT);
		panel_5.add(panel_9);

		this.btnSubmit = new JButton(Common.BTN_SUBMIT);
		panel_9.add(this.btnSubmit);
		this.btnSubmit.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
		this.btnSubmit.setName(Common.BTN_SUBMIT);
		this.btnSubmit.setPreferredSize(new Dimension(175, 75));

		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(100, 10));
		this.add(panel, BorderLayout.WEST);

		JPanel panel_1 = new JPanel();
		panel_1.setPreferredSize(new Dimension(100, 10));
		this.add(panel_1, BorderLayout.EAST);
	}

	@Override
	protected void setupListeners ()
	{
		this.btnCancel.addActionListener(this);
		this.btnSubmit.addActionListener(this);

		this.ftfCarId.addKeyListener(new KeyListener()
		{

			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String fieldCarIDText = UICheckInView.this.ftfCarId.getText();
				String fieldIDText = UICheckInView.this.ftfId.getText();

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(fieldCarIDText, fieldIDText);
				UICheckInView.this.btnSubmit.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{
			}
		});
		this.ftfId.addKeyListener(new KeyListener()
		{

			@Override
			public void keyTyped ( KeyEvent e )
			{
			}

			@Override
			public void keyReleased ( KeyEvent e )
			{
				String fieldCarIDText = UICheckInView.this.ftfCarId.getText();
				String fieldIDText = UICheckInView.this.ftfId.getText();

				boolean result = UtilityHelper.getUtilityHelperInstance().isParamsNotNull(fieldCarIDText, fieldIDText);
				UICheckInView.this.btnSubmit.setEnabled(result);
			}

			@Override
			public void keyPressed ( KeyEvent e )
			{
			}
		});

		Pattern carIDPattern = UIRootController.DEBUG
				? Pattern.compile(RegexStrings.ACCEPT_ALL)
						: Pattern.compile(RegexStrings.CAR_ID_7_DIGITS);
				this.ftfCarId.setInputVerifier(new CustomInputVerifier(this.carIDValidationLayerUI, carIDPattern));

				Pattern idPattern = UIRootController.DEBUG
						? Pattern.compile(RegexStrings.ACCEPT_ALL)
								: Pattern.compile(RegexStrings.ID_10_DIGITS);
						this.ftfId.setInputVerifier(new CustomInputVerifier(this.idValidationLayerUI, idPattern));
	}

	@Override
	protected void setupButtonsIcons ()
	{
		int iconTextGap = 20;

		ImageIcon icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_SUBMIT, 46, 46);
		if ( icon != null )
		{
			this.btnSubmit.setIcon(icon);
			this.btnSubmit.setIconTextGap(iconTextGap);
		}

		icon = UtilityHelper.getUtilityHelperInstance().loadScaledImage(Common.ICON_CANCEL, 46, 46);
		if ( icon != null )
		{
			this.btnCancel.setIcon(icon);
			this.btnCancel.setIconTextGap(iconTextGap);
		}

	}

	@Override
	public void clearView ()
	{
		this.ftfCarId.setText("");
		this.ftfId.setText("");
	}

	@Override
	public void resetView ()
	{
		this.btnSubmit.setEnabled(false);
	}

	/****************************************************************************************************************************
	 *** ActionListener methods ***
	 ****************************************************************************************************************************/
	@Override
	public void actionPerformed ( ActionEvent actionEvent )
	{
		JButton checkInViewButton = (JButton) actionEvent.getSource();
		switch ( checkInViewButton.getName() )
		{
			case Common.BTN_CANCEL:
				this.controllerDelegate.onButtonPress(Screen.ONSITE);
				break;
			case Common.BTN_SUBMIT:
				if ( this.validationListener != null )
				{
					Integer id, carid = null;
					String infoMessage = null;

					boolean verification = this.ftfId.getInputVerifier().verify(this.ftfId);
					if ( verification )
					{
						verification = this.ftfCarId.getInputVerifier().verify(this.ftfCarId);
						if ( verification )
						{
							id = Integer.parseInt(this.ftfId.getText());
							carid = Integer.parseInt(this.ftfCarId.getText());
							this.validationListener.onValidationRequest(new ValidationDescriptionEvent(this, id, carid));
						}
						else
						{
							infoMessage = String.format(UserNotification.NOT_CORRECT_FORMAT_FOR__VALUE__, this.ftfCarId.getToolTipText());
						}
					}
					else
					{
						infoMessage = String.format(UserNotification.NOT_CORRECT_FORMAT_FOR__VALUE__, this.ftfId.getToolTipText());
					}

					if ( ! verification )
					{
						this.controllerDelegate.showPopUpMessage(infoMessage, PopUpMessageType.INFORMATION);
					}
				}
				break;
		}

	}
}